﻿<?php
//This class repersents the Comments element
//Provides an interface to the comments table in the DB
//and the html page showing the comments
require_once('./inc/database.php');
 db_connect();

class comments
{
 /********member********************/
  private $comment_id;
  public $task_id;
  public $user_id;
  public $comment_time;
  public $comment_date;
  public $comment_content;
  public $read;
  public $attachments;
  
  
  /**************constrctor****************/
  public function comments()
  	{
	  $this->comment_date=date('y-m-d');
	 $time=getdate(time());
	  $this->comment_time=$time['hours'].':'.$time['minutes'].':'.$time['seconds'];
	  $this->read=0;	
	  $this->attachments="";
	}
	public function getcomment_id()
		{
		 return $this->comment_id;	
		}
	public static function  createCommentFromArray($arrCom)

	{

		$comments= new comments();

		foreach ($arrCom as $key=>$item)

		{ 

		  $comments->$key=$item;

		}

		

			return $comments;

	

	}
	public function insert()
		{
		  	 $this->comment_content=mysql_escape_string($this->comment_content);
			 $stringinsert="INSERT INTO `comments`
			 ( `task_id`, `user_id`, `comment_time`, `comment_date`, `comment_content`,`read`,`attachments`)
			  VALUES ($this->task_id,$this->user_id,'$this->comment_time','$this->comment_date',
			  '$this->comment_content',$this->read,'$this->attachments')";
			 $queryinsert=mysql_query($stringinsert) or die(mysql_error().' '.$stringinsert);
			 $this->comment_id=mysql_insert_id();
			
		}
		public function update()
		{
		   if($this->comment_id!=null || $this->comment_id!="")
		   	{  $this->comment_content=mysql_escape_string($this->comment_content);
				 $stringupdate="UPDATE `comments` SET 
				 `task_id`=$this->task_id,
				 `user_id`=$this->user_id,
				 `comment_time`='$this->comment_time',
				 `comment_date`='$this->comment_date',
				 `comment_content`='$this->comment_content',
				 `read`=$this->read,
				 `attachments`='$this->attachments'
				  WHERE `comment_id`=$this->comment_id
						  ";
			 $queryupdate=mysql_query($stringupdate) or die(mysql_error().' '.$stringupdate);
		    return true ;	
           	}
			return false;
			
		}
		public function getby_id($comment_id) //selects a comment and updates the class comments members with the selected values  
		{
		 	$stringgetby_id="SELECT * FROM `comments` WHERE comment_id=$comment_id";
			$querygetby_id=mysql_query($stringgetby_id) or die(mysql_error().' '.$stringgetby_id);
			$row=mysql_fetch_assoc($querygetby_id);
			if(mysql_affected_rows()>0)
				{
					  $this->comment_id=$comment_id;
					  $this->task_id=$row['task_id'];
					  $this->user_id=$row['user_id'];
					  $this->comment_time=$row['comment_time'];
					  $this->comment_date=$row['comment_date'];
					  $this->comment_content=$row['comment_content'];
					  $this->read=$row['read'];
					  $this->attachments=$row['attachments'];
				   return true;
				}
			return false;
		}
		public function get_html_com()
			{?>
				<div id="allcom" class="user_type<?php echo user::get_role_type_byid($this->user_id);?>">
                                  <div id="info_com">
                                      <span class="color_turkiz">
                                                <?php   echo user::get_name($this->user_id); ?>
                                     </span>
                                    </span>
                                     <span class="color_turkiz">
                                     <?php
                                                $time=explode(':',$this->comment_time);
                                                 echo $time[0].':'.$time[1];
                                     ?>
                                     </span>
                                     <span class="color_turkiz">
                                     <?php
                                              $date=$this->comment_date;
                                              $date=explode('-',$date);
                                              echo $date[2].'/'.$date[1].'/'.$date[0];
                                     ?>
                                     </span>
                                  </div>
                               <span id="print_screen_comments">
                                <?php if($this->attachments!=''){?>
                        	<a target="_blank" href="<?php echo $this->attachments; ?>" id="print_screen" class="color_turkiz">קובץ מצורף</a>
                        <?php }?>
                               </span>
						  	  <P>  <?php  echo nl2br($this->comment_content); ?></P>

                            </div>

			<?php }
	
	
}
?>